package com.dawn.login.authentication;

import org.springframework.stereotype.Component;

import weibo4j.Weibo;
import weibo4j.http.AccessToken;
import weibo4j.http.RequestToken;

@Component("sinaOAuth")
public class SinaOAuth {

    private Weibo weibo;

    public SinaOAuth() {
        weibo = new Weibo();
    }

    // 获取 requestToken
    public RequestToken getRequestToken(String callback_url) {
        try {
            RequestToken requestToken = weibo.getOAuthRequestToken(callback_url);
            System.out.println("Request token: " + requestToken.getToken());
            System.out.println("Request token secret: " + requestToken.getTokenSecret());
            return requestToken;
        } catch (Exception e) {
            System.out.println("获取Request token发生异常！");
            e.printStackTrace();
            return null;
        }
    }

    // 根据 requestToken 和 verifier 获取 accessToken
    public AccessToken gettAccessToken(RequestToken requestToken, String verifier) {
        try {
            AccessToken accessToken = weibo.getOAuthAccessToken(requestToken.getToken(), requestToken.getTokenSecret(),
                    verifier);
            System.out.println("Access token: " + accessToken.getToken());
            System.out.println("Access token secret: " + accessToken.getTokenSecret());
            return accessToken;
        } catch (Exception e) {
            System.out.println("获取Access token发生异常！");
            e.printStackTrace();
            return null;
        }
    }
}
